package cn.bugstack.springframework.test;


public class ThreadDemo {

    static class Producer extends Thread {

        private Object lock;

        public Producer(Object lock) {
            this.lock = lock;
        }

        public void run() {
            synchronized (lock) {
                try {
                    System.out.println("producer start work!");
                    sleep(10000);
                    System.out.println("producer end work!");
                    lock.notify();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class Consumer extends Thread {

        private Object lock;

        public Consumer(Object lock) {
            this.lock = lock;
        }

        public void run() {
            synchronized (lock) {
                try {
                    System.out.println("Consumer prepare to work!");
                    lock.wait();
                    System.out.println("Consumer constinue work!");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) {
        //共同的对象lock
        Object lock = new Object();
        Consumer consumer = new Consumer(lock);
        consumer.start();
        Producer producer = new Producer(lock);
        producer.start();
    }
}



